package com.tuju.jetpackfirstdemo.utils;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.tuju.jetpackfirstdemo.App;
import com.tuju.jetpackfirstdemo.MainActivity;

import java.util.List;

public class ExitUtil {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void exitAPP() {
        ActivityManager activityManager = (ActivityManager) App.instants.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.AppTask> appTaskList = activityManager.getAppTasks();
        for (ActivityManager.AppTask appTask : appTaskList) {
            appTask.finishAndRemoveTask();
        }
    }

    public static void restartApp() {
        Intent intent = new Intent(App.instants.getApplicationContext(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent restartIntent = PendingIntent.getActivity(
                App.instants.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        //退出程序
        AlarmManager mgr = (AlarmManager) App.instants.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用
    }

}
